﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Data; 
using ReflectionStudio.Core.Events;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Input;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for EditEventWindow.xaml
    /// </summary>
    public partial class TABGB52 
    {
        #region ==FROMDATE===========
        public static readonly DependencyProperty FROMDATEProperty = DependencyProperty.Register("FROMDATE",
            typeof(string), typeof(TABGB52),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnFROMDATEChanged));
        public string FROMDATE
        {
            get
            {
                return (string)GetValue(FROMDATEProperty);
            }
            set
            {
                SetValue(FROMDATEProperty, value);
            }
        }
        private static void OnFROMDATEChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TABGB52;
            if (src != null)
            {
                src.FROMDATE = (string)e.NewValue;
            }
        }
        #endregion

        #region ==ENDDATE===========
        public static readonly DependencyProperty ENDDATEProperty = DependencyProperty.Register("ENDDATE",
            typeof(string), typeof(TABGB52),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnENDDATEChanged));
        public string ENDDATE
        {
            get
            {
                return (string)GetValue(ENDDATEProperty);
            }
            set
            {
                SetValue(ENDDATEProperty, value);
            }
        }
        private static void OnENDDATEChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TABGB52;
            if (src != null)
            {
                src.ENDDATE = (string)e.NewValue;
            }
        }
        #endregion
        ITXEXRPT ITXEXRPT;
        public TABGB52()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VABPOS();
            this.DataContext = MV; 
            InitializeComponent();
            this.txtAUFKRS.Text = "RS";
            this.txtAUFLFD.Focus();
           // FROMDATE = DateTime.Today.ToShortTimeString();
            //ENDDATE = DateTime.Today.ToShortTimeString();
           
        }
      
        public override void Window_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            base.Window_Loaded(sender, e);
           
        }
      
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
           // base.vwMaster_CurrentChanged(sender, e);
           
        }

        public override void QueryCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            string A,B,C;
            if (this.ChecBox.IsChecked == false)
            {
                if (FROMDATE == "")
                {
                    FROMDATE = "20000000";
                }
                if (ENDDATE == "")
                {
                    ENDDATE = "99999999";
                }

                dwData = TBMain.NewRow();
                dwData["ABPSTARTDAT"] = FROMDATE;
                dwData["ABPENDDAT"] = ENDDATE;
                dwData["ABPFARBE"] = FABP;
                try
                {
                    SResult = TMV.QAB46(dwData);
                    MergeData(SResult, false);
                    base.QueryCommand(sender, e);
                }
                catch (Exception ex)
                {
                    Tracer.Error(ex.StackTrace, ex);
                }
            }
            else
            {
                A = "00" + txtAUFKRS.Text.Trim();
                A = A.Substring(A.Length - 2, 2);
                    B = "000000" + txtAUFLFD.Text.Trim();
                    B = B.Substring(B.Length - 6, 6);
                    C = "000" + txtAUFPOS.Text.Trim();
                    C = C.Substring(C.Length - 3, 3);
               
                
                dwData = TBMain.NewRow();
                dwData["ABPAUFKRS"] = A;
                dwData["ABPAUFLFD"] = B;
                dwData["ABPAUFPOS"] = C; 
                try
                {
                    SResult = TMV.QAB47(dwData);
                    MergeData(SResult, false);
                    base.QueryCommand(sender, e);
                }
                catch (Exception ex)
                {
                    Tracer.Error(ex.StackTrace, ex);
                }
            }
        }
        private void btnSendToExcel_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (ITXEXRPT == null)
                {
                    ITXEXRPT = new ITXEXRPT();
                    ITXEXRPT.Root = "C:\\IntexReport";
                    ITXEXRPT.strCompany = app.company;
                    ITXEXRPT.PrintType = "OpenExcel";
                }
                ITXEXRPT.dsSource = new DataSet();
                ITXEXRPT.dsSource.Merge(MV.SetProd);
                //ITXEXRPT.dsSource.Merge(Se.Env);

                ITXEXRPT.DocType = "JLTABGB52";

                ITXEXRPT.strExcelFile = @"JLT\JLTABGB52.xls";
                string filename = ITXEXRPT.WriteExcelReport();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void FROM_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                //FROMDATE = "";
                FROMDATE = Helpers.Helper.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        private void END_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                //ENDDATE = "";
                ENDDATE = Helpers.Helper.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
    }
}
